﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Secured_EditUserAccount : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Connstr"].ConnectionString);
        if (!Page.IsPostBack)
        {
            ViewState["PrevPage"] = Request.UrlReferrer;
            try
            {
                conn.Open();
                FillFormView(Convert.ToInt32(Request.QueryString["ID"]));
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
        }
    }
    private void FillFormView(int UID)
    {
        cmd = new SqlCommand("Select HAT_UserAccounts.UserID, HAT_UserAccounts.UserName, HAT_UserAccounts.IsApproved, HAT_UserAccounts.FirstName, HAT_UserAccounts.LastName, HAT_UserAccounts.DisplayName, HAT_UserAccounts.EmailAddr, HAT_UserRoles.Role from HAT_UserAccounts inner join HAT_UserRoles on HAT_UserAccounts.UserID=HAT_UserRoles.UserID where HAT_UserAccounts.UserID=@ID", conn);
        da = new SqlDataAdapter(cmd.CommandText, conn);
        da.SelectCommand.Parameters.Add("@ID", SqlDbType.Int).Value = UID;
        ds = new DataSet();
        try
        {
            da.Fill(ds, "NewTable");
            fvEditAccount.DataSource = ds.Tables["NewTable"];
            fvEditAccount.DataBind();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void btnSaveChanges_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Equals("SaveChanges"))
        {
            CheckBox chkisApproved = (CheckBox)fvEditAccount.FindControl("chkIsApproved");
            CheckBox chkForcePasswordChange = (CheckBox)fvEditAccount.FindControl("chkForcePasswordChange");
            TextBox FirstName = (TextBox)fvEditAccount.FindControl("txtFirstName");
            TextBox LastName = (TextBox)fvEditAccount.FindControl("txtLastName");
            TextBox DisplayName = (TextBox)fvEditAccount.FindControl("txtDisplayName");
            TextBox EMailAddr = (TextBox)fvEditAccount.FindControl("txtEmailAddr");
            Type cstype = this.GetType();
            cmd = new SqlCommand("update HAT_UserAccounts set FirstName=@FName, LastName=@LName, DisplayName=@DName, EmailAddr=@email, IsApproved=@Approved, ForcedPasswordChange=@ForcePassChange where UserID=@ID", conn);
            cmd.Parameters.Add("@FName", SqlDbType.VarChar).Value = FirstName.Text;
            cmd.Parameters.Add("@LName", SqlDbType.VarChar).Value = LastName.Text;
            cmd.Parameters.Add("@Dname", SqlDbType.VarChar).Value = DisplayName.Text;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = EMailAddr.Text;
            cmd.Parameters.Add("@Approved", SqlDbType.Bit).Value = chkisApproved.Checked;
            cmd.Parameters.Add("@ForcePassChange", SqlDbType.Bit).Value = chkForcePasswordChange.Checked;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Request.QueryString["ID"];
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                UpdateRole(Convert.ToInt32(Request.QueryString["ID"]));
                Page.ClientScript.RegisterClientScriptBlock(cstype, "SavedChanges", "alert('Changes saved Successfully!')", true);
                Page.Response.Redirect("~/Secured/Users.aspx", false);
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
        }
    }
    private void UpdateRole(int ID)
    {
        DropDownList UserRole = (DropDownList)fvEditAccount.FindControl("cmbUserRole");
        cmd = new SqlCommand("update HAT_UserRoles set Role=@Role where UserID=@ID", conn);
        cmd.Parameters.Add("@Role", SqlDbType.VarChar).Value = UserRole.SelectedValue;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["PrevPage"].ToString(), true);
    }
}